package com.shijimo.business.controller.admin;

import com.shijimo.server.dto.PageDto;
import com.shijimo.server.dto.ResponseDto;
import com.shijimo.server.dto.TeacherDto;
import com.shijimo.server.service.TeacherService;
import com.shijimo.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Dream_飞翔
 * @email 1072876976@qq.com
 */
@RestController
@RequestMapping("/admin/teacher")
public class TeacherController {

    private static final Logger LOG = LoggerFactory.getLogger(TeacherController.class);
    public static final String BUSINESS_NAME = "";

    @Resource
    private TeacherService teacherService;

    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        LOG.info("pageDto：{}", pageDto);
        // 进行查询
        teacherService.list(pageDto);
        // 设置返回前端的对象，默认成功
        responseDto.setContent(pageDto);
        return responseDto;
    }

    @PostMapping("/save")
    public ResponseDto save(@RequestBody TeacherDto teacherDto) {
        LOG.info("teacherDto：{}", teacherDto);

        // 保存校验
        ValidatorUtil.require(teacherDto.getName(), "姓名");
        ValidatorUtil.length(teacherDto.getName(), "姓名", 1, 50);
        ValidatorUtil.length(teacherDto.getNickname(), "昵称", 1, 50);
        ValidatorUtil.length(teacherDto.getImage(), "头像", 1, 100);
        ValidatorUtil.length(teacherDto.getPosition(), "职位", 1, 50);
        ValidatorUtil.length(teacherDto.getMotto(), "座右铭", 1, 50);
        ValidatorUtil.length(teacherDto.getIntro(), "简介", 1, 500);
        ResponseDto responseDto = new ResponseDto();
        // 在数据持久层中进行数据的存储
        teacherService.save(teacherDto);
        responseDto.setContent(teacherDto);
        return responseDto;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        LOG.info("id：{}", id);
        // 执行删除方法
        teacherService.delete(id);
        return responseDto;
    }

    @PostMapping("/all")
    public ResponseDto all() {
        ResponseDto responseDto = new ResponseDto();
        // 进行查询
        List<TeacherDto> teacherDtoList = teacherService.all();
        // 设置返回前端的对象，默认成功
        responseDto.setContent(teacherDtoList);
        return responseDto;
    }
}
